package com.jt.controller;

import com.jt.pojo.Teacher;
import com.jt.pojo.User;
import com.jt.service.TeacherService;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/findAll")
    public List<Teacher> findAll(){

        return teacherService.findAll();
    }

    /**
     * 业务说明:
     *  1. /user/list
     *  2.请求类型: GET
     *  3.参数接收: 后台使用PageResult对象接收
     *  3.返回值: SysResult<PageResult>
     */
    @GetMapping("/list")
    public SysResult getUserList(PageResult pageResult){//参数3
        //业务查询总数.分页条数.
        pageResult = teacherService.getUserList(pageResult);
        System.out.println(pageResult.getQuery());
        return SysResult.success(pageResult);//参数5个

    }

    /**
     * 根据ID查询数据库
     * URL:/user/{id}
     * 参数: id
     * 返回值: SysResult(user对象)
     */
    @GetMapping("/{id}")
    public SysResult findUserById(@PathVariable Integer id){

        Teacher teacher = teacherService.findUserById(id);
        return SysResult.success(teacher);
    }

    /**
     * 业务说明: 实现数据的修改操作
     * URL:  /user/updateUser
     * 参数:  user对象
     * 返回值: SysResult对象
     * 请求类型: PUT
     */
    @PutMapping("/updateTeacher")
    public SysResult updateUser(@RequestBody Teacher teacher){
        teacherService.updateUser(teacher);
        return SysResult.success();
    }

    /**
     * 关于请求的小结
     *    1.常规请求方式 get/delete   ?key=value&key2=value2
     *    2.post/put    data: JS对象    后端接收@RequestBody
     *    3.restFul风格  /url/arg1/arg2/arg3   使用对象接收
     * 完成用户删除操作
     *  1.URL地址 /user/{id}
     *  2.参数:  id
     *  3.返回值: SysResult
     */
    @DeleteMapping("/{id}")
    public SysResult deleteUserById(@PathVariable Integer id){
        teacherService.deleteUserById(id);
        return SysResult.success();
    }


}
